package com.ruoyi.web.controller.api;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ImageUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Api(tags = "上传图片")
@RestController
@RequestMapping("/api/fileData")
public class FileController {

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private ImageUtils imageUtils;


    @ApiOperation(value = "七牛云上传文件",notes = "七牛云上传文件")
    @PostMapping("qiNiuUpload")// 接口名称和描述
    public AjaxResult image(@RequestParam(value = "file", required = false) MultipartFile file
    ) {
        if (file==null) {
            return  AjaxResult.error("上传图片为空！");
        }
        Map<String,String> url =  imageUtils.uploadImageQiniu(file);
        return AjaxResult.success(url);
    }


    @RequestMapping(value = "/qiNiuImageMp4",method = RequestMethod.POST)
    @ApiOperation(value = "七牛云上传视频",notes = "七牛云上传视频")    // 接口名称和描述
    public AjaxResult qiNiuImageMp4(@RequestParam(value = "file", required = false) MultipartFile file
    ) {
        if (file==null) {
            return AjaxResult.error("上传视频为空！");
        }
        Map<String,String>  url =  imageUtils.uploadImageQiniuMP4(file);
        return AjaxResult.success(url);
    }

    @ApiOperation("上传图片")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "视频上传")
    @PostMapping("/uploadVideo")
    public AjaxResult uploadVideo(MultipartFile file) throws Exception
    {
        if (file == null) {
            System.out.println("File is null");
        } else if (file.isEmpty()) {
            System.out.println("File is empty");
        } else {
            System.out.println("File is not empty");
            System.out.println("File name: " + file.getOriginalFilename());
            System.out.println("File size: " + file.getSize() + " bytes");
            System.out.println("Content type: " + file.getContentType());
        }

        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.uploadVideo(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
